﻿using System;
using System.Collections.Generic;
using System.Collections;
//using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SpicIE;

namespace AutoFillPluginIE
{
    public partial class AutoFillToolbar : SpicIE.Controls.Toolbar
    {
        public override string PluginGuid
        {
            get { return "33739C34-6B2E-44b7-ABBD-D56EB700947F"; }
        }

        // The PluginProgID _must_ be provided to identify and register the COM object correctly
        public override string PluginProgID
        {
            get { return "AutoFillPluginIE.AutoFillToolbar"; }
        }

        public AutoFillToolbar()
        {
            InitializeComponent();
            this.ToolbarName = "AutoFill IE";
            this.ToolbarTitle = "AutoFill Plugin";

            this.ToolbarHelpText = "This is a sample for a vertical MyNamespace.MyToolbar";
            this.ToolbarStyle = ToolbarEnum.ExplorerToolbar;

            // represents the startup size of the toolbar
            this.Size = new Size(30, 30);
            // represents the sizing steps
            this.IntegralSize = new Size(0, 0);
            // represents the minimum size
            this.MinSize = new Size(20, 20);
            // represents the maximum size
            this.MaxSize = new Size(600, 600);

            // demonstrates how to set the backgroundcolor for the toolbar
            //this.BackgroundColor = Color.White;
            this.btnFill.UseVisualStyleBackColor = true;            

            // creates a new control collection and inserts the designer-generated controls
            Control[] ctrls = new Control[Controls.Count];
            Controls.CopyTo(ctrls, 0);

            // call internal method for implementing controls in the toolbar
            BuildControls(ctrls);

        }

        Thread socketThread = null;

        private void btnFill_Click(object sender, EventArgs e)
        {                           
            SHDocVw.IWebBrowser2 browser = AutoFillPluginIE.HostInstance.BrowserRef as SHDocVw.IWebBrowser2;
            if (browser != null)
            {
                // get the IHTMLDocument interface
                mshtml.IHTMLDocument2 htmlDoc = browser.Document as mshtml.IHTMLDocument2;
                mshtml.HTMLDocument htmlDoc2 = browser.Document as mshtml.HTMLDocument;
                if (htmlDoc != null)
                {                                        
                    ArrayList result = new ArrayList();                    

                    mshtml.IHTMLElementCollection inputs = htmlDoc2.getElementsByTagName("input");

                    //mshtml.IHTMLElementCollection title = htmlDoc2.getElementsByTagName("title");
                    //MessageBox.Show(htmlDoc2.title);
                    int n = inputs.length;
                    String encode_title = EncodeTo64(htmlDoc2.title);
                    result.Add("url:" + encode_title);                    
                    
                    for (int i = 0; i < n; i++)
                    {
                        mshtml.HTMLInputElement temp = inputs.item(i, i) as mshtml.HTMLInputElement;                        
                        String type = temp.getAttribute("type", 0).ToString();                        
                        if ( type == "text" || type =="password")
                        {                            
                            //MessageBox.Show(temp.getAttribute("name",1).ToString());
                            try
                            {
                                String name = temp.getAttribute("name", 0).ToString();
                                String encode_name = EncodeTo64(name);
                                result.Add(name);                                
                            }
                            catch (NullReferenceException ex)
                            {                                
                            }                            
                            //temp.setAttribute("value", "a" + i.ToString(), 0);                            
                        }                                                                                   
                    }
                    
                    
                    
                    //MessageBox.Show(requestString);

                    socketThread = new Thread(new ThreadStart(AutoFillSender.SendMessage));
                    
                    AutoFillSender.messages = result;
                    AutoFillSender.url = browser.LocationURL;
                    AutoFillSender.isSubmit = chkBoxSubmit.Checked;
                    
                    socketThread.Start();
                    

                    //AutoFillSender.SendMessage(requestString);
                                       
                    /*
                    mshtml.IHTMLElementCollection submits = htmlDoc2.getElementsByName("btnG");
                    MessageBox.Show(submits.length.ToString());
                    mshtml.HTMLInputElement submit = submits.item(0,0) as mshtml.HTMLInputElement;
                    submit.click();
                     */

                }
            }                  
            
        }

        static public string EncodeTo64(string toEncode)
        {

            byte[] toEncodeAsBytes

                  = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);

            string returnValue

                  = System.Convert.ToBase64String(toEncodeAsBytes);

            return returnValue;

        }

        static public string DecodeFrom64(string encodedData)
        {

            byte[] encodedDataAsBytes

                = System.Convert.FromBase64String(encodedData);

            string returnValue =

               System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

            return returnValue;

        }
    }
}
